package net.wenku8.reader.view.user

import androidx.lifecycle.viewModelScope
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.wenku8.reader.enums.KeyLoginPassword
import net.wenku8.reader.enums.KeyLoginRememberPassword
import net.wenku8.reader.enums.KeyLoginUsername
import net.wenku8.reader.enums.KeyOnlineReadHis
import net.wenku8.reader.enums.KeySettingBg
import net.wenku8.reader.enums.KeySettingFont
import net.wenku8.reader.enums.KeySettingSpace
import net.wenku8.reader.service.FileService
import net.wenku8.reader.view.common.BaseViewModel

class CacheClearViewModel : BaseViewModel() {

    fun clearAll(){
        viewModelScope.launch(Dispatchers.IO){
            MMKV.defaultMMKV().clearAll()
            FileService.deleteFile("/setting/")
            FileService.deleteFile("/book/")
            FileService.deleteFile("/wenku8/")
            showSnackbar("清除完成")
        }
    }

    fun clearCustom(){
        viewModelScope.launch(Dispatchers.IO){
            MMKV.defaultMMKV().removeValuesForKeys(arrayOf(KeySettingFont, KeySettingBg, KeySettingSpace))
            FileService.deleteFile("/setting/")
            showSnackbar("清除完成")
        }
    }
    fun clearBookFile(){
        viewModelScope.launch(Dispatchers.IO){
            FileService.deleteFile("/book/")
            showSnackbar("清除完成")
        }
    }
    fun clearWenku8Book(){
        viewModelScope.launch(Dispatchers.IO){
            MMKV.defaultMMKV().allKeys()?.forEach {
                if (it.startsWith(KeyOnlineReadHis)){
                    MMKV.defaultMMKV().remove(it)
                }
            }
            FileService.deleteFile("/wenku8/")
            showSnackbar("清除完成")
        }
    }
    fun clearWenku8Account(){
        viewModelScope.launch(Dispatchers.IO){
            MMKV.defaultMMKV().removeValuesForKeys(arrayOf(KeyLoginUsername, KeyLoginPassword, KeyLoginRememberPassword))
            showSnackbar("清除完成")
        }
    }
}